<?php
/**
 *author:韩帅
 *createtime:2017/6/19 14:11
 *description:
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Shop extends Base
{


    /**
     * 定义已删除的状态
     */
    const ISDELETE_YES  = 1;

    /**
     * 定义未删除的状态
     */
    const ISDELETE_NO   = 0;

    const STATUS_BEGIN       = 1; //启用
    const STATUS_STOP        = 2; //停用

    protected $table = 'wd_system_shop';

    public static $msg = ''; // 提示语
    
    //获得login号/商户号
    public function getLogin() {

        $login = $this->max('login');
        $login = ($login - 80000) >= 0 ?  ( $login+1 ) : 80000;
        return $login;
    }


    /**
     * 加减账户金额
     * @param  [type] $shopid [店铺id]
     * @param  [type] $money  [金钱数]
     * @param  [type] $type   [fr 分润金， zt 助推金]
     * @param  [type] $type   [add 加/ red 减]
     * @return [type]         
     */
    public static function changeMoney($shopid, $money, $type='zq', $class = 'add') {

        $shop = self::find($shopid);

        if ( $type == 'fr' ) {
            $class == 'add' &&  $shop->money += $money;
            $class == 'red' &&  $shop->money -= $money;
        }

        if ( $type == 'zt' ) {
            $class == 'add' &&  $shop->help_money += $money;
            $class == 'red' &&  $shop->help_money -= $money;
        }

        $result = $shop->save();

        return ($result == true) ? true : false;
    }


    /**
     * 提现减账户金额
     * @param  [type] $shopid [店铺id]
     * @param  [type] $money  [金钱数]
     * @return [type]         
     */
    public static function redMoney($shopid, $money) {

        $shop = self::find($shopid);
        //判断提现金额是否合法
        
        if ( ($shop->money + $shop->help_money) < $money) {
            self::$msg = '提现金额不合法';
            return false;
        }

        //判断提现金额是否大于分润金
        if ($shop->money >= $money) {
            $shop->money -= $money; 
        } else {

            $shop->money = 0 ;

            $shop->help_money -= ($money - $shop->money);
        }

        $result = $shop->save();

        return ($result == true) ? true : false;
    }
}